﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Base.Objects;
using ERP_RORZE_PERMISSION.Forms;

namespace ERP_RORZE_PERMISSION.UserControls
{
    public partial class ucUserGroup : DevExpress.XtraEditors.XtraUserControl
    {
        private int index = 0;
        private LogUtils log;
        public int _action = 1;

        public ucUserGroup()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void ucUser_Load(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        private void btAddNew_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            EditRow();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            DeleteRows();
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditRow();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteRows();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {

            var gridView = (GridView)sender;
            var point = gridView.GridControl.PointToClient(Control.MousePosition);

            DoDoubleClick(gridView, point);
        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {

            if (gridView1.RowCount != 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        #region Private methods

        public void BindDataGrid()
        {
            gridControl1.DataSource = BaseDAO.ExecuteQuery("Select * from [222_UserGroup] order by [GroupName]");
            gridView1.BestFitColumns();
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
        }

        private void AddNew()
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_UserGroupAdd(this);
            fr.StartPosition = FormStartPosition.CenterScreen;
            fr.Show();
            Cursor.Current = Cursors.Default;

        }

        private void EditRow()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                frm_UserGroupEdit fr = new frm_UserGroupEdit(this);
                var row = new DataRow[gridView1.SelectedRowsCount];
                row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);

                index = gridView1.FocusedRowHandle;
                _action = 1;

                int groupId = int.Parse(row[0]["GroupID"].ToString());
                DataTable dt = BaseDAO.ExecuteQuery("Select * from [222_UserGroup]  where [GroupID] =" + groupId);

                fr.GroupID = groupId;
                fr.GroupName = dt.Rows[0]["GroupName"].ToString();
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Show();
            }
            else
            {
                MessageBox.Show(PermissionConstants.Message.TableIsNull, PermissionConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void DoDoubleClick(GridView v, Point p)
        {
            _action = 1;
            index = gridView1.FocusedRowHandle;
            var info = v.CalcHitInfo(p);
            if (info.InRow || info.InRowCell)
            {
                frm_UserGroupEdit fr = new frm_UserGroupEdit(this);
                var row = new DataRow[gridView1.SelectedRowsCount];
                row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);

                index = gridView1.FocusedRowHandle;
                _action = 1;

                int groupId = int.Parse(row[0]["GroupID"].ToString());
                DataTable dt = BaseDAO.ExecuteQuery("Select * from [222_UserGroup]  where [GroupID] =" + groupId);

                fr.GroupID = groupId;
                fr.GroupName = dt.Rows[0]["GroupName"].ToString();
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Show();
            }
        }

        private void DeleteRows()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                var row = new DataRow[gridView1.SelectedRowsCount];
                var groupId = new string[gridView1.SelectedRowsCount];

                for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                {
                    row[i] = gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
                    groupId[i] = row[i]["GroupID"].ToString();
                }


                if (MessageBox.Show(PermissionConstants.Message.DeleteQuestion, PermissionConstants.Form.MessageBox.Warning,
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    for (int i = 0; i < groupId.Length; i++)
                    {

                        if (BaseDAO.ExecuteNonQuery("Delete from [222_UserGroup] where [GroupID]=" + groupId[i]) > 0)
                        {

                            //write Log
                            log.Table = LogConstant.Table.SystemAdmin.UserGroups;
                            log.Task = LogConstant.Task.Delete;
                            log.Description = "GroupID: " + row[i]["GroupID"] + "; GroupName: " + row[i]["GroupName"];
                            log.WriteLog();

                            _action = 2;
                        }
                        else
                        {
                            MessageBox.Show(PermissionConstants.Message.DeleteFailed, PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    BindDataGrid();


                }
            }
            else
            {
                MessageBox.Show(PermissionConstants.Message.TableIsNull, PermissionConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }


        #endregion

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            frm_Login login = new frm_Login();
            login.StartPosition = FormStartPosition.CenterScreen;
            login.Show();
        }




    }
}
